﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        //埃氏筛法寻找(a,b)之间所有素数
        int[] findPrime(int a, int b)
        {
            int[] isPrime = new int[b + 1];
            for (int i = 2; i <= b; i++) isPrime[i] = 1;
            for (int i = 2; i * i <= b; i++)
            {
                if (isPrime[i] == 1)
                {
                    for (int j = i * i; j <= b; j += i) isPrime[j] = 0;
                }
            }
            return isPrime;
        }
        public Form1()
        {
            InitializeComponent();

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox3.Text = "";
            int max = int.Parse(this.textBox1.Text);
            int min = int.Parse(this.textBox2.Text);
            int[] isPrime = findPrime(min, max);
            int index = 0;

            for (int i = min; i <= max; i++)
            {

                if (isPrime[i] == 1)
                {
                    this.textBox3.Text += i + " ";
                    index++;
                }
                if (index >= 10)
                {
                    index = 0;
                    this.textBox3.Text += "\r\n";
                }

            }
        }
    }
}
